/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mobcatweb;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Label;
import com.sun.webui.jsf.component.Tab;
import com.sun.webui.jsf.component.TabSet;
import com.sun.webui.jsf.component.Tree;
import com.sun.webui.jsf.component.TreeNode;
import java.util.Iterator;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import persistencia.entidade.Categoria;
import persistencia.entidade.PortalCfg;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version Page1.java
 * @version Created on Jun 11, 2009, 6:53:39 PM
 * @author victorino
 */
public class Page1 extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private HtmlPanelGrid panelPrincipal = new HtmlPanelGrid();

    public HtmlPanelGrid getPanelPrincipal() {
        return panelPrincipal;
    }

    public void setPanelPrincipal(HtmlPanelGrid hpg) {
        this.panelPrincipal = hpg;
    }
    private HtmlPanelGrid panelCategoria = new HtmlPanelGrid();

    public HtmlPanelGrid getPanelCategoria() {
        return panelCategoria;
    }

    public void setPanelCategoria(HtmlPanelGrid hpg) {
        this.panelCategoria = hpg;
    }
    private HtmlPanelGrid panelRegistro = new HtmlPanelGrid();

    public HtmlPanelGrid getPanelRegistro() {
        return panelRegistro;
    }

    public void setPanelRegistro(HtmlPanelGrid hpg) {
        this.panelRegistro = hpg;
    }
    private PortalCfg portal;
    private Tree tree1 = new Tree();

    public Tree getTree1() {
        return tree1;
    }

    public void setTree1(Tree t) {
        this.tree1 = t;
    }
    private Label lbCategoriaSelecionada = new Label();

    public Label getLbCategoriaSelecionada() {
        return lbCategoriaSelecionada;
    }

    public void setLbCategoriaSelecionada(Label l) {
        this.lbCategoriaSelecionada = l;
    }
    // </editor-fold>

    public Page1() {
    }
    private List<Categoria> subCategoriaList = null;

    @Override
    public void init() {
        super.init();
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

        // </editor-fold>
        portal = getSessionBean1().getPortalJpa().getActivePortal();
        loadTree();
        panelCategoria.setRendered(false);
        panelPrincipal.setRendered(false);
        panelRegistro.setRendered(false);
    }

    @Override
    public void prerender() {
    }

    private void loadTree() {
        List<Categoria> cats = getSessionBean1().getCatJpa().findCategoriaRaizEntities();
        for (int i = 0; i < cats.size(); i++) {
            Categoria categoria = cats.get(i);
            TreeNode t = new TreeNode();
            t.setText(categoria.getNome());
            t.setId("_" + categoria.getCategoriaId());
            t.setActionExpression(FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
                    createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{Page1.tree_action}", null, new Class<?>[0]));
            preencheCategoriasFilhas(t, categoria);
            tree1.getChildren().add(t);
        }
    }

    private void preencheCategoriasFilhas(TreeNode node, Categoria raiz) {

        List<Categoria> cats = raiz.getCategoriaCollection();
        for (Iterator<Categoria> it = cats.iterator(); it.hasNext();) {
            Categoria categoria = it.next();

            TreeNode t = new TreeNode();
            t.setText(categoria.getNome());
            t.setId("_" + categoria.getCategoriaId());
            t.setActionExpression(FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
                    createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{Page1.tree_action}", null, new Class<?>[0]));
            preencheCategoriasFilhas(t, categoria);
            node.getChildren().add(t);
        }
    }

    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public PortalCfg getPortal() {
        return portal;
    }

    public void setPortal(PortalCfg portal) {
        this.portal = portal;
    }

    public void tree_action() {
        try {

            String catId = tree1.getSelected();
            catId = catId.substring(1, catId.length());
            Integer id = Integer.parseInt(catId);
            Categoria cat = getSessionBean1().getCatJpa().findCategoria(id);
            panelCategoria.setRendered(true);
            lbCategoriaSelecionada.setText(cat.getResumoFilhos());

        } catch (Exception es) {
        }
    }
    /*
    public String tab1_action() {
    Tab tab = tabSet1.getSelectedTab();
    if (tab.getId().equals("tab1")) {
    }
    return null;
    }
     */

    @Override
    public void preprocess() {
    }

    @Override
    public void destroy() {
    }

    /**
     * @return the subCategoriaList
     */
    public List<Categoria> getSubCategoriaList() {
        return subCategoriaList;
    }

    /**
     * @param subCategoriaList the subCategoriaList to set
     */
    public void setSubCategoriaList(List<Categoria> subCategoriaList) {
        this.subCategoriaList = subCategoriaList;
    }
}

